from django.http import HttpResponse
from django.shortcuts import render
from .models import Comment
from .forms import CommentForm
from django.views.generic.base import View
from blog.models import Blog
from comments.models import Comment
from django.shortcuts import get_object_or_404, redirect, render


# Create your views here.


class CommentView(View):

    def post(self, request, id, *args, **kwargs):
        blog = get_object_or_404(Blog, id=id)
        comment_form = CommentForm(request.POST)
        print(f'返回的表单是否绑定了数据：{comment_form.is_bound}')
        print(f'返回的表单数据：{request.POST}')
        print(f'数据有效性验证：{comment_form.is_valid()}')
        print(f'错误信息：{comment_form.errors}')
        print(f'{CommentForm(data=request.POST)}')
        if comment_form.is_valid():
            nickname = comment_form.cleaned_data['nickname']
            email = comment_form.cleaned_data['email']
            content = comment_form.cleaned_data['content']
            print(f'{nickname},{email},{content}')
            comment = Comment.objects.create(nickname=nickname, email=email, content=content, post=blog)
            return redirect(blog)
        else:
            msg = '表单提交错误，请重新输入。。。'
        return render(request, 'blog.html', locals())
